package org.fatmansoft.teach.repository;

import org.fatmansoft.teach.models.AbsenceRecord;
import org.fatmansoft.teach.models.StudentLog;
import org.fatmansoft.teach.service.AbsenceRecordService;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AbsenceRecordRepository extends JpaRepository<AbsenceRecord,Integer> {

    @Query(value = "SELECT MAX(absenceRecordId) FROM AbsenceRecord")
    Integer getMaxId();

    //根据studentId查找该学生的所有日志信息
    @Query("SELECT s FROM AbsenceRecord s " +
            "WHERE (:teacherId IS NULL OR s.teacher.id = :teacherId) " +
            "AND (:studentId IS NULL OR s.student.id = :studentId) " + // 假设 AbsenceRecord 有一个与 Student 关联的字段
            "AND (:startDate IS NULL OR s.date >= :startDate) " +
            "AND (:endDate IS NULL OR s.date <= :endDate) " +
            "AND (:courseId IS NULL OR s.course.id = :courseId)")
    List<AbsenceRecord> findByTeacherIdAndStudentIdAndDateStringBetweenAndCourseId(
            @Param("teacherId") Integer teacherId,
            @Param("studentId") Integer studentId,
            @Param("startDate") String startDate, // 应为'YYYY-MM-DD'格式的字符串
            @Param("endDate") String endDate,    // 应为'YYYY-MM-DD'格式的字符串
            @Param("courseId") Integer courseId);


    void deleteByAbsenceRecordId(Integer absenceRecordId);
}